<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="[[${data.namespace}]]">
  <resultMap id="BaseResultMap" type="[[${data.baseResultMap.type}]]">
    <id column="[[${data.baseResultMap.id.columnName}]]" jdbcType="[[${data.baseResultMap.id.jdbcType}]]" property="[[${data.baseResultMap.id.propertyName}]]" />[# th:each="item : ${data.baseResultMap.results}"]
    <result column="[[${item.columnName}]]" jdbcType="[[${item.jdbcType}]]" property="[[${item.propertyName}]]" />[/]
  </resultMap>
  <sql id="BaseColumnList">
    [[${data.baseColumnList}]]
  </sql>
  <sql id="BaseWhereClause">
    <where >
	    <foreach collection="columns" index="index" item="item">
	        and ${item.name} = #{item.value} 
	    </foreach>  
	    <foreach collection="likeColumns" index="index" item="item">
	        and ${item.name} like #{item.value} 
	    </foreach>  
	    <foreach collection="betweenColumns" index="index" item="item">
	        and ${item.name} between #{item.start} and #{item.end} 
	    </foreach> 
	    <foreach collection="compareColumns" index="index" item="item">
	        and ${item.name} #{item.value}
	    </foreach> 
	    <foreach collection="inColumns" index="index" item="item">
	        and ${item.name} in 
	        <foreach collection="item.value" index="index" item="it" open="(" separator="," close=")">
		        #{it}
		    </foreach> 
	    </foreach> 
    </where>
  </sql>
  <insert id="insert" parameterType="[[${data.baseResultMap.type}]]" useGeneratedKeys="true" keyProperty="[[${data.baseResultMap.id.propertyName}]]">
    insert into [[${data.tableName}]]
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="[[${data.baseResultMap.id.propertyName}]] != null" >
        [[${data.baseResultMap.id.columnName}]],
      </if>[# th:each="item : ${data.baseResultMap.results}"]
      <if test="[[${item.propertyName}]] != null" >
        [[${item.columnName}]],
      </if>[/]
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="[[${data.baseResultMap.id.propertyName}]] != null" >
        #{[[${data.baseResultMap.id.propertyName}]]},
      </if>[# th:each="item : ${data.baseResultMap.results}"]
      <if test="[[${item.propertyName}]] != null" >
        #{[[${item.propertyName}]]},
      </if>[/]
    </trim>
  </insert>
  <insert id="insertBatch" parameterType="java.util.List">
    insert into [[${data.tableName}]] ([[${data.baseColumnList}]])
    values 
    <foreach collection ="list" item="item" index= "index" separator =",">
	(#{item.[[${data.baseResultMap.id.propertyName}]]}[# th:each="item : ${data.baseResultMap.results}"], #{item.[[${item.propertyName}]]}[/])
    </foreach >
  </insert>
  <update id="update" parameterType="[[${data.baseResultMap.type}]]">
    update [[${data.tableName}]]
    <set>[# th:each="item : ${data.baseResultMap.results}"]
      <if test="[(${item.propertyName})] != null">
        [(${item.columnName})] = #{[(${item.propertyName})]},
      </if>
    [/]</set>
    where [[${data.baseResultMap.id.columnName}]] = #{[[${data.baseResultMap.id.propertyName}]]}
  </update>
  <update id="updateByParams" parameterType="[[${data.baseMapperParamsType}]]">
    update [[${data.tableName}]]
    <set>
      <foreach collection="updateColumns" index="index" item="item" separator=",">
	    ${item.name} = #{item.value} 
	  </foreach>  
    </set>
    <include refid="BaseWhereClause" />
  </update>
  <delete id="deleteByPrimaryKey" parameterType="[[${data.baseResultMap.id.propertyType}]]">
    delete from [[${data.tableName}]]
    where [[${data.baseResultMap.id.columnName}]] = #{primaryKey}
  </delete>
  <delete id="deleteByParams" parameterType="[[${data.baseMapperParamsType}]]">
    delete from [[${data.tableName}]]
	<include refid="BaseWhereClause" /> 
  </delete>
  <select id="selectByPrimaryKey" parameterType="[[${data.baseResultMap.id.propertyType}]]" resultMap="BaseResultMap">
    select 
    <include refid="BaseColumnList" />
    from [[${data.tableName}]]
    where [[${data.baseResultMap.id.columnName}]] = #{primaryKey}
  </select>
  <select id="selectByParams" resultMap="BaseResultMap" parameterType="[[${data.baseMapperParamsType}]]" >
    select
    <include refid="BaseColumnList" />
    from [[${data.tableName}]]
    <include refid="BaseWhereClause" />
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
    limit #{offset},#{rows}
  </select>
  <select id="selectOneByParams" resultMap="BaseResultMap" parameterType="[[${data.baseMapperParamsType}]]" >
    select
    <include refid="BaseColumnList" />
    from [[${data.tableName}]]
    <include refid="BaseWhereClause" />
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
    limit 1
  </select>
  <select id="selectCountByParams" resultType="java.lang.Integer" parameterType="[[${data.baseMapperParamsType}]]" >
    select
      count(0)
    from [[${data.tableName}]]
    <include refid="BaseWhereClause" />
  </select>
  
  <!-- O(∩_∩)O -->
  [# th:utext="${data.customCode}" /]
</mapper>